from django.shortcuts import render, HttpResponse

# Create your views here.
import random
# from chinesename import ChineseName
from student import models


def create_person_default(request):
    return create_person(request, 1)


def create_person(request, count):
    objs = models.Person.objects.all()
    if objs:
        response = HttpResponse()
        response.write('<h1>表格必须为空</h1>')
        response.write('<a href="/student/emptyperson/">清空记录</a>')
        return response
    # cn = ChineseName()
    # for i in range(count):
    #     dic = {
    #         'gender': random.choice([1, 1, 1, 2, 2]),
    #         'age': random.randint(18, 60),
    #         'universe': random.choice([1, 2, 3]),
    #         'bounty': '%.2f' % random.uniform(1000, 999999),
    #     }
    #     dic['name'] = cn.getName(random.choice([1, 1, 1, 2, 2]),
    #                              sex='boy' if dic['gender'] == 1 else 'girl'
    #                              )
    #     string = ''
    #     for i in range(random.randint(10, 20)):
    #         current = random.randint(97, 122)  # ASCII表中这个范围是小写字母
    #         string = "%s%s" % (string, chr(current))  # 用chr将数字根据ASCII转成字母
    #     dic['comment'] = string
    #     models.Person.objects.create(**dic)
    return HttpResponse("%s条记录添加完毕" % count)


def empty_person(request):
    objs = models.Person.objects.all()
    response = HttpResponse()
    if objs:
        objs.delete()
        response.write('<h1>表格已经清空</h1>')
    else:
        response.write('<h1>表格原本就是空的</h1>')
    response.write('<a href="/student/createperson/500">重新生成500条记录</a>')
    response.write('<p>创建上百一次记录，时间会比较长</p>')
    return response


